﻿<!DOCTYPE html>
<html>
	<head>
		<title>插件控制台</title>
        $css()
	</head>
	<body>
	<style type="text/css">
         .item
        {
            margin: 15px 5px;
            padding:10px 100px 10px 120px;
            clear: both;
            position: relative;
            border-bottom:solid 1px #d0d0d0;
            height:80px;
          
        }
        .item div.meta{position:absolute;bottom:15px;left:120px;}
        .item img.icon
        {
            width:80px;
            height:80px;
            float: left;
            position: absolute;
            left: 10px;
        }
        .item span.btns{position:absolute;right:10px;top:10px;width:60px;}
        .item span.btns a{display:block;padding:2px 0;text-align:center;font-weight:bold;background:#ffff00;color:#ff6600;background:#006699;color:#fff;}
        .item span.btns a.run{background:#ffff00;color:#ff6600;display:none;}
        .item span.btns a.config{background:#006699;color:#fff;}
        .item span.btns a.uninstall{background:#d0d0d0;color:#fff;}
        .item h3
        {
            margin:0;
            padding: 0;
            font-size: 16px;
            color:#0066cc;
        }
        .item p.descript{color:#666;}
    </style>
    
    
		<div class="plugins" id="plugins">
        	加载中...
		</div>
		
		$js()
    <script type="text/javascript">
        var appPath = './';
        var currentSum = 0;
        var singleGetNum = 10;
        var win={x:800,y:600};

        var mp = J.$('plugins');

        //,sp=J.$('sub_control'),ft=J.$('form_tpl');

        /*
        function loads(){
        J.xhr.post('gets','num='+ singleGetNum +'&from='+currentSum,function(result){
        mp.innerHTML=result;
        });
        }
		
        loads();
        */

        //初始化事件
        function initEvents() {

            J.each(document.getElementsByClassName('handler'), function (i, e) {
                if (e.href == 'plugin:list') {
                    e.onclick = function () {
                        mp.className = '';
                        sp.className = 'hidden';
                        return false;
                    };
                } else if (e.href == 'plugin:upload') {
                    e.onclick = function () {
                        mp.className = 'hidden';
                        sp.className = '';

                        sp.innerHTML = J.template(ft.innerHTML, { indent: '', name: '', icon: '', zip: '' });

                        //初始化
                        J.validator.init();

                        J.$('tb_name').onchange = function () {
                            var _name = this.value;
                            window.iconUpload = J.upload({ debug: true, id: 'up_icon', url: 'upload/icon?id=iconUpload&name=' + _name }, function (path) {
                                J.$('tb_icon').value = path;
                            });

                            window.zipUpload = J.upload({ id: 'up_zip', url: 'upload/zip?id=zipUpload&name=' + _name }, function (path) {
                                J.$('tb_zip').value = path;
                            });
                        };
                        return false;
                    };
                } else if (e.href.indexOf('plugin:run') != -1) {
                    e.onclick = function () {
                        var match = /plugin:run\('([^']+)','([^']+)'\)/.exec(e.href);
                        J.lazyRun(function () {

                            var d = J.dialog('', true, false);
                            var title = decodeURIComponent(match[1]) +
                             '<a style="position:absolute;right:80px;top:0px" href="' + match[2] + '" target="_blank">新窗口运行</a>' +
                             '<a href="javascript:;" style="position:absolute;right:40px;top:0px" ' +
                             'onclick="fullRun(\'' + d.id + '\',\'' + match[1] + '\',\'' + match[2] + '\')">全屏</a>';

                            d.title = title;
                            d.open(match[2], win.x, win.y);
                        });
                        return false;
                    };
                } else if (e.href.indexOf('plugin:config') != -1) {
                    e.onclick = function () {
                        var match = /plugin:config\('([^']+)','([^']*)'\)/.exec(e.href);
                        J.lazyRun(function () {
                            var d = J.dialog(decodeURIComponent(match[1]) + '-设置', true, false);
                            d.open(match[2], win.x, win.y);
                        });
                        return false;
                    };
                }else if(e.href.indexOf('plugin:chstat')!=-1){
                	 e.onclick = function () {
                	 	var t=this;
                	 	var ts=t.getAttribute('stat');
                        var match = /plugin:chstat\('([^']+)'\)/.exec(e.href);
                        J.xhr.post(appPath,'module=plugin&action=changestate&worker_indent='+match[1],function(json){
                        	t.innerHTML=ts=='1'?'启用':'停用';
                        	t.setAttribute('stat',ts=='1'?'0':'1');
                        });
                        return false;
                    };
                }else if(e.href.indexOf('plugin:uninstall')!=-1){
                	 e.onclick = function () {
                	 	var t=this;
                        var match = /plugin:uninstall\('([^']+)'\)/.exec(e.href);
                        J.xhr.post(appPath,'module=plugin&action=uninstall&worker_indent='+match[1],function(json){
                        	location.reload();
                        });
                        return false;
                    };
                }
            });
        }




        J.__WORKPATH__ = '/framework/common/scripts/';

        var jsModule = new Array('dialog');
        for (var i in jsModule) {
            J.ld(jsModule[i]);
        }

        function fullRun(id,title, url) {
            var d=window['dialog_'+id];
            if (d) {
                d.close();
            }
            J.lazyRun(function () {
                var d2 = J.dialog(title, true, false);
                d2.close(url, 800, 600);
            });
        }

        function createItem(p) {
            var e = document.createElement('DIV');
            e.className = 'item';

            var indent = p.key,
				icon = p.properties[3].descript,
				version = p.properties[0].descript,
				author = p.properties[2].descript,
				state = p.properties[1].descript,
				webpage = p.properties[4].descript,
				portalUrl = p.properties[5].descript,
				configUrl = p.properties[6].descript,
				descript= p.descript==''?'无说明': p.descript;

            var html = '<img src="'+ appPath +'module=plugin&action=geticon&worker_indent=' + p.key + '" class="icon" alt=""/><h3 class="name">' + p.name + '</h3>' +
						'<p class="descript">' + descript + '</p><div class="meta">作者：<span class="author">' + author + '</span>&nbsp;&nbsp;版本：<span class="version">' +
 						version + '</span></div><span class="btns">' +
						'<a class="handler enable" href="plugin:chstat(\'' + indent + '\')" stat="'+state+'">' + (state == '1' ? '停用' : '启用') + '</a>';

            if (portalUrl != '') {
                html += '<a class="handler run" href="plugin:run(\'' + p.name + '\',\'' + portalUrl + '\')">运行</a>'
            }

            if (configUrl != '') {
                html += '<a class="handler config" href="plugin:config(\'' + p.name + '\',\'' + configUrl + '\')">设置</a>';
            }
            
            html+='<a class="handler uninstall" href="plugin:uninstall(\'' + indent + '\')">卸载</a></span>';
            
            e.innerHTML = html;

            mp.appendChild(e);
        }

        function loadLocal() {
            J.xhr.request({ uri: appPath, params: 'module=plugin&action=getpluginsJson', method: 'POST', data: 'json' },
               {
                   success: function (json) {
                       mp.innerHTML = '';
                       for (var p in json) {
                           createItem(json[p]);
                       }
                       
                       if(json.length==0){
                       	mp.innerHTML='<div><br /><br /><center>未安装任何插件</center></div>';
                       }
                       
                       initEvents();
                   }
               });
        }

        loadLocal();
        
        function install(url){
        	J.xhr.post(appPath,'module=plugin&action=install&url='+url,function(json){
        		alert('安装成功')
        	});
        }
    </script>
	
	</body>
</html>